﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Skyworth.CRM.Common
{
    public class Pagination
    {
        /// <summary>
        /// 返回指定页数据的SQL
        /// </summary>
        /// <param name="sql">原始SQL语句</param>
        /// <param name="startIndex">起始记录</param>
        /// <param name="endIndex">截止记录</param>
        /// <returns></returns>
        public static string PageSql(string sql, string orderKey, int startIndex, int endIndex)
        {
            const string COL_ROWNUM = "COL_ROWNUM"; //SQL内部使用
            const string TAB_PAGESQL = "TAB_PAGESQL";
            return string.Format("SELECT * FROM (SELECT ROW_NUMBER() Over(ORDER BY {5}) AS {3},{4}.* FROM ({0}) {4}) tmpTable WHERE {3} >= {1} AND {3} <= {2}",
                sql, startIndex, endIndex, COL_ROWNUM, TAB_PAGESQL, orderKey);
        }
    }
}